﻿import QtQuick 2.12
import QtCharts 2.3
import QtQuick.Controls 2.12

ChartView {
    id: view
    title: "逆变器考核分分布"
    titleColor: "white"
    anchors.fill: parent
    antialiasing: true
  //  theme: ChartView.ChartThemeDark
    legend.labelColor:"white"
    legend.markerShape:Legend.MarkerShapeRectangle
    legend.alignment: Qt.AlignBottom
    ValueAxis {
        id: xAxis
        min: 0
        max: 6
        labelsColor: "#13B2E8"
        gridLineColor: "#13B2E8"
        color: "#13B2E8"
    }
    ValueAxis {
        id: yAxis
        min: 0
        max: 25
        labelsColor: "#13B2E8"
        gridLineColor: "#13B2E8"
        color: "#13B2E8"
    }

    Component.onCompleted: {
        let plants = query.getPlants();
        plants = JSON.parse(plants);
        plants.forEach(appendScatterByName);
    }

    function appendScatterByName(plantName) {
        let scatter = view.createSeries(ChartView.SeriesTypeScatter, "scatter", xAxis, yAxis);
        scatter.useOpenGL = true;
        let score = query.getScore(plantName);
        let obj = JSON.parse(score);
        for (var prop in obj) {
            scatter.append(obj[prop].month, obj[prop].score);
        }
        scatter.hovered.connect(hoverInfo);
    }

    function setColor() {
        var colors = ["#1abc9c", "#e67e22", "#B33771","#e74c3c"];
        let count = pieSeries.count;
        for(let i = 0; i < count; i++) {
            pieSeries.at(i).color = colors[i];
        }
    }
    ToolTip {
        id: toolTipPop
        parent: view
        background: Rectangle {
            color: "#393232"
            radius: 5
        }
        contentItem: Text {
            text: toolTipPop.text
            color: "white"
            font.pixelSize: 15
            padding: 5
            lineHeight: 1.4
        }
    }
    function hoverInfo(point, state) {
        toolTipPop.visible = state;
        if (state) {
            let pos = view.mapToPosition(point, view.series("scatter"));
            toolTipPop.x = pos.x + 10;
            toolTipPop.y = pos.y - toolTipPop.height / 2;
            toolTipPop.text = "横坐标%1\n".arg(point.x) + "纵坐标%1".arg(point.y);
        }
    }
}
